﻿namespace 燃油加注机指令翻译
{
    internal class SendPrice
    {
        [HexMapping(0, 6)]
        public string Header { get; set; } = "FC FC FC";

        [HexMapping(6, 2)]
        public string Control { get; set; } = "03";

        [HexMapping(8, 2)]
        public string FuelGun { get; set; } = "01";

        [HexMapping(10, 6)]
        public string? Price { get; set; }

        [HexMapping(16, 8)]
        public string? Reciprocal { get; set; }

        [HexMapping(24, 2)]
        public string? CheckSum { get; set; }

        public string instructionsHexSimple()
        {
            return Header + " " + Control + " " + FuelGun + " " +  Price + " " + Reciprocal;
        }

        public string instructionsHex()
        {
            return instructionsHexSimple() + " " + CalculateChecksum(instructionsHexSimple());
        }


        string CalculateChecksum(string controlStr)
        {
            byte[] data = Tool.HexStringToByteArray(controlStr.Replace(" ", "").Substring(6));
            byte checksum = 0;
            foreach (byte b in data)
            {
                checksum ^= b;
            }
            return checksum.ToString("X2");
        }

    }
}
